package com.yykj.base.server.service.impl;

import com.yykj.base.client.dto.WordBriefInfo;
import com.yykj.base.server.model.entity.WordDictionary;
import com.yykj.base.server.model.repository.WordDictionaryRepository;
import com.yykj.base.server.service.WordService;
import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.GsonUtil;
import com.yykj.common.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WordServiceImpl implements WordService {
    @Override
    public String getWuBiFromBaiDu(String word) throws Exception {

        String baseUrl = "https://hanyu.baidu.com/s?wd={keyword}&from=zici";
        String requestUrl = baseUrl.replace("{keyword}", word);
        String str = HttpUtil.get(requestUrl);
        int index = str.indexOf("五 笔");
        int start = index + 42;
        int end = start + 4;
        String result = str.substring(start, end);
        if (result.contains("<")) {
            result = result.replace("<", "");
        }
        return result;

    }

    @Override
    public WordBriefInfo getWordBriefInfo(String word) {
        if (StringUtils.isEmpty(word)) {
            throw new NormalException(ResultEnums.PARAM_ERROR);
        }
        WordBriefInfo wordBriefInfo = new WordBriefInfo();
        String[] split = word.split("");

        List<WordDictionary> wordDictionaryList = wordDictionaryRepository.findAllByZiIn(Arrays.asList(split));
        Map<String, WordDictionary> map = new HashMap<>();
        for (WordDictionary wordDictionary : wordDictionaryList) {
            map.put(wordDictionary.getZi(), wordDictionary);
        }
        wordBriefInfo.setWord(word);
        for (String s : split) {
            WordDictionary wordDictionary = map.get(s);
            if (wordDictionary != null) {
                wordBriefInfo.setPy(wordBriefInfo.getPy() + wordDictionary.getPy());
                wordBriefInfo.setWb(wordBriefInfo.getWb() + wordDictionary.getWubi());
            } else {
                wordBriefInfo.setPy(wordBriefInfo.getPy() + s);
                wordBriefInfo.setWb(wordBriefInfo.getWb() + s);
            }
        }

        return wordBriefInfo;
    }

    public void intiWuBi() {
        WordDictionary wordDictionary = wordDictionaryRepository.findFirstByWubi("");
        if (wordDictionary != null) {
            log.info("找到：{}", GsonUtil.toJson(wordDictionary));
            try {
                String wuBiFromBaiDu = getWuBiFromBaiDu(wordDictionary.getZi());
                if (wuBiFromBaiDu != null) {
                    log.info("五笔：{} = {}", wordDictionary.getZi(), wuBiFromBaiDu);
                    wordDictionary.setWubi(wuBiFromBaiDu);
                    wordDictionaryRepository.save(wordDictionary);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private WordDictionaryRepository wordDictionaryRepository;

    @Autowired
    public void setWordDictionaryRepository(WordDictionaryRepository wordDictionaryRepository) {
        this.wordDictionaryRepository = wordDictionaryRepository;
    }

}
